//
//  SXBaseNavController.swift
//  sina
//
//  Created by shengxinwei on 16/5/20.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit

class SXBaseNavController: UINavigationController,UIGestureRecognizerDelegate {
 
    override func viewDidLoad() {
        super.viewDidLoad()
        //解决右滑返回手势的问题
        //存在bug 在最顶级的页面 右滑返回手势还会响应
        self.interactivePopGestureRecognizer?.delegate = self
    }
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        return childViewControllers.count > 1
    }
    
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        if childViewControllers.count > 0 {
            var backTitle = "返回"
            if childViewControllers.count == 1 {
                 backTitle = childViewControllers.first?.title ?? "返回"
            }
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title:backTitle, target: self, action: #selector(backToFront))
            
            viewController.navigationItem.leftBarButtonItem = back
        }
        
        super.pushViewController(viewController, animated: true)
        
    }
    
    //pop
    @objc private func backToFront() {
        popViewControllerAnimated(true)
    }
    
   

}
